/*
 * @Author: Du.M
 * @Date: 2021-07-01 16:19:13
 * @LastEditTime: 2021-08-04 14:11:29
 * @FilePath: \express_basics\service\loginService.js
 */
const { querySql } = require("../utils")
const { getToken,decode }  = require("../utils/user-jwt")


const loginTest = (req,res,next) =>{
    res.status(200).json({
      'status':200
    })
}
const login = (req, res, next) => {
    const { body } = req
    res.status(200).json({
      'status':200
    })
    return
    console.log(body)
    const sql = `select * from user where user_name='${body.userName}' and pass_word='${body.password}'`
    try {
      querySql(sql).then(result => {
        console.log(result)
        if(result.length > 0){
          if (result[0].user_name === body.userName && result[0].pass_word === body.password) {
            const token = getToken(body.userName)
            res.status(200).json({
              'status': 'ok',
              token,
              'user':{
                  username:body.userName,
                  password:body.password
              }
            })
          }else{
            res.status(200).json({
              'status':'error'
            })
          }
        }else{
          res.status(200).json({
            'status':'error'
          })
        }
      })
    } catch (error) {
      res.status(500).send("数据库连接失败")
    }
  
  
  }
  module.exports = {
      login,
      loginTest,
  }